package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.itheima.core.controller.AbstractCoreController;

import java.util.Map;

/**
 * @description <p>自媒体图文内容信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmNewsController",tags = "自媒体图文内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }

    /**
     * 发表文章
     * @param dto
     * @param draft
     * @return
     */
    @PostMapping("/submit")
    public ResultVo submit(@RequestBody WmNewsDtoSave dto, boolean draft){
        System.out.println("draft= " + draft);
        Long newsId = wmNewsService.submit(dto, draft);
        return ResultVo.ok(newsId);
    }

    /**
     * 文章列表分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResultVo pageList(@RequestBody WmNewsPageReqDto dto){
        return wmNewsService.pageList(dto);
    }

    /**
     * 通过id查询文章信息
     * @param newsId
     * @return
     */
    @GetMapping("/one/{newsId}")
    public ResultVo getOne(@PathVariable(value = "newsId") Long newsId){
        return ResultVo.ok(wmNewsService.getById(newsId));
    }

    /**
     * 通过id删除文章
     * @param newsId
     * @return
     */
    @GetMapping("/del_news/{newsId}")
    public ResultVo deleteById(@PathVariable(value = "newsId") Long newsId){
        wmNewsService.deleteById(newsId);
        return ResultVo.ok();
    }

    /**
     * 文章上下架
     * @param paramMap
     * @return
     */
    @PostMapping("/down_or_up")
    public ResultVo down_or_up(@RequestBody Map<String,Integer> paramMap){
        wmNewsService.downOrUn(paramMap);
        return ResultVo.ok();
    }
}

